package com.mys2sh.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.mys2sh.model.User;
import com.mys2sh.service.UserService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class UserValidInterceptor extends AbstractInterceptor {

	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();

		User cookieUser = (User) request.getAttribute("currentUser");
		
		String uid = cookieUser.getUid();

		if (uid != null) {
			User currentUser = userService.getUserInfoByUid(Integer.parseInt(uid));
			if (currentUser.getUid() != null) {
				return actionInvocation.invoke();
			}
		}

		return Action.LOGIN;
	}

}
